package com.lyl.momanapp.frament;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.GradientDrawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lyl.momanapp.R;
import com.lyl.momanapp.adapter.OneOneAdapter;
import com.lyl.momanapp.adapter.OneOneimgAdapter;
import com.lyl.momanapp.bean.Material;
import com.lyl.momanapp.bean.Tag;
import com.lyl.momanapp.bean.URL;
import com.lyl.momanapp.util.picker.ColorPickerView;
import com.lyl.momanapp.util.picker.StickerView;
import com.lyl.momanapp.util.utlis.MyUtils;
import com.lyl.momanapp.util.view.MyFragment;
import com.lyl.momanapp.util.view.PaletteView;
import com.lyl.momanapp.util.view.PasterView.PasterView;

import org.xutils.x;

import java.util.ArrayList;

/**
 * 名字 梁亚龙
 * 时间 2017/9/11.
 * QQ邮箱 503934353@qq.com
 */

public class OneTwo extends MyFragment {

    String TAG = "OneTwo";

    private LinearLayout mSucai, mHuaBi;
    private View mSucaiIN;
    private View mHuabiIN;
    private PaletteView mPaletteView;
    private LinearLayout mEraser;
    private SeekBar mHuaBiSize, mHuaBiTouming;
    private RecyclerView mRl;
    private RecyclerView mRlimg;
    private ImageView mImg;
    private OneOneAdapter mOneOneAdapter;
    private OneOneimgAdapter mOneOneimgAdapter;
    private TextView mSZ;
    private TextView mColor;
    private RelativeLayout mRl1;
    private LinearLayout mRL2;
    private Button mBut;
    private Button mZiTiBut;
    private GradientDrawable drawable;
    private GradientDrawable drawable_ziti;
    private SeekBar mColorSet;
    private RelativeLayout mRlimgseji;
    private PasterView pasterView;
    private ImageView mBackView;//背景图
    private StickerView mStickerView;//添加贴画的图层
    private AlertDialog mTextDialog;//输入文本dialog
    private EditText mDialogInput;//输入文本的ed
    private int mTextColor = Color.BLACK;//最终的text的颜色
    int defaultHeight, defaultWidth;// 屏幕宽高
    private View mDrawImage;//最终保存图片区域
    private LinearLayout wenzi;
    private View mZiTi;
    private View xiangpicha;
    private TextView mZiTibainji;
    private TextView mZiTiColor;
    private LinearLayout mBianji;
    private LinearLayout mZiTiColorLL;
    private SeekBar mZitiColorSet;
    private Boolean zitizhuantai = false;

    Button select_col_1;
    Button select_col_2;
    Button select_col_3;
    Button select_col_4;
    Button select_col_5;
    Button select_col_6;
    Button select_col_7;
    Button select_col_8;
    Button select_col_9;


    Button select_col_1_wz;
    Button select_col_2_wz;
    Button select_col_3_wz;
    Button select_col_4_wz;
    Button select_col_5_wz;
    Button select_col_6_wz;
    Button select_col_7_wz;
    Button select_col_8_wz;
    Button select_col_9_wz;

    @Override
    public int inflaterRootView() {
        return R.layout.frament_one_two;
    }

    @Override
    public void initUI() {
        mSucai = ((LinearLayout) findView(R.id.one_two_sucai));
        mHuaBi = ((LinearLayout) findView(R.id.one_two_huabi));
        mSZ = ((TextView) findView(R.id.huabi_sezi));
        mColor = ((TextView) findView(R.id.huabi_color_but));
        mZiTibainji = ((TextView) findView(R.id.ziti_bianji));
        mZiTiColor = ((TextView) findView(R.id.ziti_color_but));
        mBianji = ((LinearLayout) findView(R.id.ziti_ll1));
        mZiTiColorLL = ((LinearLayout) findView(R.id.ziti_ll2));
        mRl1 = ((RelativeLayout) findView(R.id.huabi_rl1));
        mRL2 = ((LinearLayout) findView(R.id.huabi_ll2));
        mHuabiIN = findView(R.id.one_two_huabigone);
        mSucaiIN = findView(R.id.one_two_sucaigone);
        mZiTi = findView(R.id.one_two_ziti);
        xiangpicha = findView(R.id.one_two_xiangpica);
        mEraser = (LinearLayout) findView(R.id.one_two_eraser);
        wenzi = ((LinearLayout) findView(R.id.one_two_wenzi));
        mStickerView = findView(R.id.id_stickerew);
        mPaletteView = (PaletteView) findView(R.id.palette);
        mHuaBiSize = ((SeekBar) findView(R.id.layout_hua_bi_size));
        mHuaBiTouming = ((SeekBar) findView(R.id.layout_hua_bi_touming));
        mRl = ((RecyclerView) findView(R.id.lian_dong_rl_name));
        mRlimg = ((RecyclerView) findView(R.id.lian_dong_rl_img));
        mImg = ((ImageView) findView(R.id.one_two_img));
        mColorSet = ((SeekBar) findView(R.id.huabi_yan_se_size));
        mZitiColorSet = ((SeekBar) findView(R.id.ziti_yan_se_size));
        mBut = ((Button) findView(R.id.huabi_color));
        mZiTiBut = ((Button) findView(R.id.ziti_color));

        mRlimgseji = findView(R.id.one_two_img_rl);
        drawable = (GradientDrawable) mBut.getBackground();

        drawable_ziti = (GradientDrawable) mZiTiBut.getBackground();


        Display currentDisplay = getActivity().getWindowManager().getDefaultDisplay();
        defaultHeight = currentDisplay.getHeight();
        defaultWidth = currentDisplay.getWidth();


        select_col_1 = findView(R.id.select_col_1);
        select_col_2 = findView(R.id.select_col_2);
        select_col_3 = findView(R.id.select_col_3);
        select_col_4 = findView(R.id.select_col_4);
        select_col_5 = findView(R.id.select_col_5);
        select_col_6 = findView(R.id.select_col_6);
        select_col_7 = findView(R.id.select_col_7);
        select_col_8 = findView(R.id.select_col_8);
        select_col_9 = findView(R.id.select_col_9);


        select_col_1_wz = findView(R.id.select_col_1_wz);
        select_col_2_wz = findView(R.id.select_col_2_wz);
        select_col_3_wz = findView(R.id.select_col_3_wz);
        select_col_4_wz = findView(R.id.select_col_4_wz);
        select_col_5_wz = findView(R.id.select_col_5_wz);
        select_col_6_wz = findView(R.id.select_col_6_wz);
        select_col_7_wz = findView(R.id.select_col_7_wz);
        select_col_8_wz = findView(R.id.select_col_8_wz);
        select_col_9_wz = findView(R.id.select_col_9_wz);

    }

    public void shuaxin() {
        Log.i("1", "shuaxin: " + URL.ONEONEimg);

        Glide.with(getActivity()).load(URL.ONEONEimg).into(mImg);
    }

    public void seji() {
        String image = new MyUtils().saveImage(mRlimgseji, new MyUtils().getTime(), getContext());
        URL.sejiURL = image;

    }


    @Override
    public void initData() {

    }


    @Override
    public void addListeners() {
        mPaletteView.setMode(PaletteView.Mode.ERASER);
        mSZ.setOnClickListener(this);
        mColor.setOnClickListener(this);
        mZiTibainji.setOnClickListener(this);
        mZiTiColor.setOnClickListener(this);
        mSucai.setOnClickListener(this);
        mHuaBi.setOnClickListener(this);
        mEraser.setOnClickListener(this);
        wenzi.setOnClickListener(this);
        mPaletteView.setOnClickListener(this);
        Setsucai();
        sethuabi();
        setziti();


        select_col_1.setOnClickListener(this);
        select_col_2.setOnClickListener(this);
        select_col_3.setOnClickListener(this);
        select_col_4.setOnClickListener(this);
        select_col_5.setOnClickListener(this);
        select_col_6.setOnClickListener(this);
        select_col_7.setOnClickListener(this);
        select_col_8.setOnClickListener(this);
        select_col_9.setOnClickListener(this);


        select_col_1_wz.setOnClickListener(this);
        select_col_2_wz.setOnClickListener(this);
        select_col_3_wz.setOnClickListener(this);
        select_col_4_wz.setOnClickListener(this);
        select_col_5_wz.setOnClickListener(this);
        select_col_6_wz.setOnClickListener(this);
        select_col_7_wz.setOnClickListener(this);
        select_col_8_wz.setOnClickListener(this);
        select_col_9_wz.setOnClickListener(this);

    }

    private void setziti() {
        mZitiColorSet.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean b) {
                int color = 0;
                if (progress <= 255) {
                    color = Color.rgb(255, progress, 0);
                } else if (progress > 255 && progress <= 510) {
                    int a = 255 - (progress - 255);
                    color = Color.rgb(a, 255, 0);
                } else if (progress > 510 && progress <= 765) {
                    color = Color.rgb(0, 255, progress - 510);
                } else if (progress > 765 && progress <= 1020) {
                    color = Color.rgb(0, 255 - (progress - 765), 255);
                } else if (progress > 1020 && progress <= 1275) {
                    color = Color.rgb(progress - 1020, 0, 255);
                } else if (progress > 1275 && progress <= 1530) {
                    color = Color.rgb(255, 0, 255 - (progress - 1275));
                }
                mTextColor = color;
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }

    private void Setsucai() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
        mRl.setLayoutManager(linearLayoutManager);
        mRl.setHasFixedSize(true);
        mOneOneAdapter = new OneOneAdapter(new String[]{"1", "2", "3", "3", "3", "3", "3"});
        mRl.setAdapter(mOneOneAdapter);
        Material.ResultBean.DataBean dataBean = new Material.ResultBean.DataBean();
        dataBean.setTag_name("推荐");
        ArrayList<Material.ResultBean.DataBean> dataBeans = new ArrayList<>();
        dataBeans.add(dataBean);
        mOneOneAdapter.showData(dataBeans);
        mOneOneAdapter.setOnItemClickLitener(new OneOneAdapter.OnItemClickLitener() {
            @Override
            public void onItemClick(TextView view, int position) {

            }
        });


        LinearLayoutManager linearLayoutManager2 = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
        mRlimg.setLayoutManager(linearLayoutManager2);
        mRlimg.setHasFixedSize(true);
        mOneOneimgAdapter = new OneOneimgAdapter(new String[]{"1", "2", "3", "3", "3", "3", "3"});
        mRlimg.setAdapter(mOneOneimgAdapter);

        mOneOneimgAdapter.setOnItemClickLitener(new OneOneimgAdapter.OnItemClickLitener() {
            @Override
            public void onItemClick(ImageView view, Tag.DataBean dataBean, int p) {

                x.image().bind(mImg, "https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png");

            }
        });
    }

    private void sethuabi() {
        mColorSet.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                int color = 0;
                Log.e(TAG, "progress   " + progress);
                if (progress <= 255) {
                    color = Color.rgb(255, progress, 0);
                } else if (progress > 255 && progress <= 510) {
                    int a = 255 - (progress - 255);
                    color = Color.rgb(a, 255, 0);
                } else if (progress > 510 && progress <= 765) {
                    color = Color.rgb(0, 255, progress - 510);
                } else if (progress > 765 && progress <= 1020) {
                    color = Color.rgb(0, 255 - (progress - 765), 255);
                } else if (progress > 1020 && progress <= 1275) {
                    color = Color.rgb(progress - 1020, 0, 255);
                } else if (progress > 1275 && progress <= 1530) {
                    color = Color.rgb(255, 0, 255 - (progress - 1275));
                }

                mPaletteView.setPenColor(color);
                drawable.setColor(color);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        mHuaBiSize.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                Log.i("11", "onProgressChanged: " + progress);

                mPaletteView.setPenRawSize(progress);
                mPaletteView.setMode(PaletteView.Mode.ERASER);
                mPaletteView.setMode(PaletteView.Mode.DRAW);
                mPaletteView.setEraserSize(progress + 20);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        mHuaBiTouming.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                mPaletteView.setPenAlpha(progress);
                mPaletteView.setMode(PaletteView.Mode.ERASER);
                mPaletteView.setMode(PaletteView.Mode.DRAW);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.one_two_sucai: {
                zitizhuantai = false;
                mSucaiIN.setVisibility(View.VISIBLE);
                mHuabiIN.setVisibility(View.GONE);
                mZiTi.setVisibility(View.GONE);
                xiangpicha.setVisibility(View.GONE);
                mSucai.setBackgroundColor(Color.parseColor("#E3E3E3"));
                mHuaBi.setBackgroundColor(Color.parseColor("#FFFFFF"));
                mEraser.setBackgroundColor(Color.parseColor("#FFFFFF"));
                wenzi.setBackgroundColor(Color.parseColor("#FFFFFF"));
            }
            break;
            case R.id.one_two_huabi: {
                zitizhuantai = false;
                mPaletteView.setMode(PaletteView.Mode.DRAW);
                mHuabiIN.setVisibility(View.VISIBLE);
                mSucaiIN.setVisibility(View.GONE);
                mZiTi.setVisibility(View.GONE);
                xiangpicha.setVisibility(View.GONE);
                mSucai.setBackgroundColor(Color.parseColor("#FFFFFF"));
                mHuaBi.setBackgroundColor(Color.parseColor("#E3E3E3"));
                mEraser.setBackgroundColor(Color.parseColor("#FFFFFF"));
                wenzi.setBackgroundColor(Color.parseColor("#FFFFFF"));

            }
            break;
            case R.id.one_two_wenzi: {
                createTextDialog();
                zitizhuantai = true;
                mZiTi.setVisibility(View.VISIBLE);
                mSucaiIN.setVisibility(View.GONE);
                mHuabiIN.setVisibility(View.GONE);
                xiangpicha.setVisibility(View.GONE);
                mSucai.setBackgroundColor(Color.parseColor("#FFFFFF"));
                mHuaBi.setBackgroundColor(Color.parseColor("#FFFFFF"));
                mEraser.setBackgroundColor(Color.parseColor("#FFFFFF"));
                wenzi.setBackgroundColor(Color.parseColor("#E3E3E3"));
            }
            break;
            case R.id.one_two_eraser: {
                zitizhuantai = false;
                xiangpicha.setVisibility(View.VISIBLE);
                mSucaiIN.setVisibility(View.GONE);
                mHuabiIN.setVisibility(View.GONE);
                mZiTi.setVisibility(View.GONE);
                mPaletteView.setMode(PaletteView.Mode.ERASER);
                mSucai.setBackgroundColor(Color.parseColor("#FFFFFF"));
                mHuaBi.setBackgroundColor(Color.parseColor("#FFFFFF"));
                mEraser.setBackgroundColor(Color.parseColor("#E3E3E3"));
                wenzi.setBackgroundColor(Color.parseColor("#FFFFFF"));
            }
            break;
            case R.id.huabi_color_but: {
                mRl1.setVisibility(View.GONE);
                mRL2.setVisibility(View.VISIBLE);
                mColor.setBackgroundColor(0xE3E3E3);
                mSZ.setBackgroundColor(0xFFFFFFFF);
            }
            break;
            case R.id.huabi_sezi: {
                mColor.setBackgroundColor(0xFFFFFFFF);
                mSZ.setBackgroundColor(0xE3E3E3);
                mRl1.setVisibility(View.VISIBLE);
                mRL2.setVisibility(View.GONE);
            }
            break;
            case R.id.ziti_bianji: {
                mBianji.setVisibility(View.VISIBLE);
                mZiTiColorLL.setVisibility(View.GONE);
                mZiTibainji.setBackgroundColor(0xE3E3E3);
                mZiTiColor.setBackgroundColor(0xFFFFFFFF);
            }
            break;
            case R.id.ziti_color_but: {
                mZiTibainji.setBackgroundColor(0xFFFFFFFF);
                mZiTiColor.setBackgroundColor(0xE3E3E3);
                mZiTiColorLL.setVisibility(View.VISIBLE);
                mBianji.setVisibility(View.GONE);
            }
            break;
//            case R.id.palette:{
//                if (zitizhuantai) {
//                    createTextDialog();
//                }
//            }

            case R.id.select_col_1:
                int color = Color.rgb(255, 255, 255);
                mPaletteView.setPenColor(color);
                drawable.setColor(color);
                break;
            case R.id.select_col_2:
                int color2 = Color.rgb(0, 0, 0);
                mPaletteView.setPenColor(color2);
                drawable.setColor(color2);
                break;
            case R.id.select_col_3:
                int color3 = Color.rgb(180, 60, 46);
                mPaletteView.setPenColor(color3);
                drawable.setColor(color3);
                break;
            case R.id.select_col_4:
                int color4 = Color.rgb(102, 44, 42);
                mPaletteView.setPenColor(color4);
                drawable.setColor(color4);
                break;
            case R.id.select_col_5:
                int color5 = Color.rgb(175, 206, 80);
                mPaletteView.setPenColor(color5);
                drawable.setColor(color5);
                break;
            case R.id.select_col_6:
                int color6 = Color.rgb(84, 178, 164);
                mPaletteView.setPenColor(color6);
                drawable.setColor(color6);
                break;
            case R.id.select_col_7:
                int color7 = Color.rgb(204, 64, 186);
                mPaletteView.setPenColor(color7);
                drawable.setColor(color7);
                break;
            case R.id.select_col_8:
                int color8 = Color.rgb(245, 254, 85);
                mPaletteView.setPenColor(color8);
                drawable.setColor(color8);
                break;
            case R.id.select_col_9:
                int color9 = Color.rgb(77, 96, 201);
                mPaletteView.setPenColor(color9);
                drawable.setColor(color9);
                break;


            case R.id.select_col_1_wz:
                int color_wz = Color.rgb(255, 255, 255);
                mTextColor = color_wz;
                mPaletteView.setPenColor(mTextColor);
                drawable_ziti.setColor(mTextColor);
                break;
            case R.id.select_col_2_wz:
                int color2_wz = Color.rgb(0, 0, 0);
                mTextColor = color2_wz;
                mPaletteView.setPenColor(mTextColor);
                drawable_ziti.setColor(mTextColor);
                break;
            case R.id.select_col_3_wz:
                int color3_wz = Color.rgb(180, 60, 46);
                mTextColor = color3_wz;
                mPaletteView.setPenColor(mTextColor);
                drawable_ziti.setColor(mTextColor);
                break;
            case R.id.select_col_4_wz:
                int color4_wz = Color.rgb(102, 44, 42);
                mTextColor = color4_wz;
                mPaletteView.setPenColor(mTextColor);
                drawable_ziti.setColor(mTextColor);
                break;
            case R.id.select_col_5_wz:
                int color5_wz = Color.rgb(175, 206, 80);
                mTextColor = color5_wz;
                mPaletteView.setPenColor(mTextColor);
                drawable_ziti.setColor(mTextColor);
                break;
            case R.id.select_col_6_wz:
                int color6_wz = Color.rgb(84, 178, 164);
                mTextColor = color6_wz;
                mPaletteView.setPenColor(mTextColor);
                drawable_ziti.setColor(mTextColor);
                break;
            case R.id.select_col_7_wz:
                int color7_wz = Color.rgb(204, 64, 186);
                mTextColor = color7_wz;
                mPaletteView.setPenColor(mTextColor);
                drawable_ziti.setColor(mTextColor);
                break;
            case R.id.select_col_8_wz:
                int color8_wz = Color.rgb(245, 254, 85);
                mTextColor = color8_wz;
                mPaletteView.setPenColor(mTextColor);
                drawable_ziti.setColor(mTextColor);
                break;
            case R.id.select_col_9_wz:
                int color9_wz = Color.rgb(77, 96, 201);
                mTextColor = color9_wz;
                mPaletteView.setPenColor(mTextColor);
                drawable_ziti.setColor(mTextColor);
                break;
        }

    }


    //输入文本的dialog
    private void createTextDialog() {

        final AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        LinearLayout view = (LinearLayout) LayoutInflater.from(getContext()).inflate(R.layout.dialog_inputext_drawimage, null);
        mDialogInput = (EditText) view.findViewById(R.id.ed_dialog_input);
//        ColorPickerView colorPickerView = new ColorPickerView(getContext(), defaultWidth / 2, defaultWidth / 2);
//        colorPickerView.setmListener(colorChangeListenre);
//        view.addView(colorPickerView);
        mDialogInput.setTextColor(mTextColor);
        mTextDialog = builder.setView(view).setTitle("请输入内容").
                setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        try {
                            String text = mDialogInput.getText().toString();
                            DisplayMetrics dm = new DisplayMetrics();
                            dm = getResources().getDisplayMetrics();
                            float density = dm.density;  // 屏幕密度
                            Bitmap bitmap = Bitmap.createBitmap((int) (text.length() * density * 35),
                                    (int) (70 * density), Bitmap.Config.ARGB_8888);
                            Canvas canvas = new Canvas(bitmap);
                            Paint paint = new Paint();
                            paint.setColor(mTextColor);
                            paint.setTextSize(35 * density);
                            canvas.drawText(text, 0, 35 * density, paint);
                            mStickerView.addBitImage(bitmap);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }).setNegativeButton("取消", null).create();
        mTextDialog.show();


    }

    ColorPickerView.OnColorChangedListener colorChangeListenre = new ColorPickerView.OnColorChangedListener() {
        @Override
        public void colorChanged(int color) {
            mTextColor = color;
            mDialogInput.setTextColor(color);
        }
    };
}
